package com.anolesoft.twelvePlan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.anolesoft.twelvePlan.dto.MesPlanJiedianDTO;
import com.anolesoft.twelvePlan.entity.MesPlanJiedianDO;
import com.anolesoft.twelvePlan.mapper.MesPlanJiedianMapper;
import com.anolesoft.twelvePlan.service.MesPlanJiedianService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zyq
 * @date 2022/8/10
 */
@Service
@Slf4j
public class MesPlanJiedianServiceImpl implements MesPlanJiedianService {
    @Autowired
    private MesPlanJiedianMapper mesPlanJiedianMapper;
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public PageData<List<MesPlanJiedianDTO>> queryJiedian(MesPlanJiedianDTO mesPlanJiedianDTO, Integer pageNo, Integer pageSize) {
        Page<MesPlanJiedianDO> page = new Page<>(pageNo, pageSize);
        List<MesPlanJiedianDO> mesPlanJiedianDOList = this.mesPlanJiedianMapper.queryJiedian(MesPlanJiedianDO.builder()
                .jx(CommonUtils.stringNull(mesPlanJiedianDTO.getJx()))
                .partid(CommonUtils.stringNull(mesPlanJiedianDTO.getPartid()))
                .build(), page);
        List<MesPlanJiedianDTO> mesPlanJiedianDTOList = mesPlanJiedianDOList.stream()
                .map(t -> BeanUtil.copyProperties(t, MesPlanJiedianDTO.class))
                .collect(Collectors.toList());
        return new PageData<>(((int) page.getTotal()), mesPlanJiedianDTOList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertJiedian(MesPlanJiedianDTO mesPlanJiedianDTO) {
        int repeat = this.mesPlanJiedianMapper.repeatJiedian(MesPlanJiedianDO.builder()
                .jx(CommonUtils.stringNull(mesPlanJiedianDTO.getJx()))
                .partid(CommonUtils.stringNull(mesPlanJiedianDTO.getPartid()))
                .gongxuId(CommonUtils.stringNull(mesPlanJiedianDTO.getGongxuId()))
                .build());
        int count = 0;
        if (repeat == 0) {
            count = this.mesPlanJiedianMapper.insertJiedian(MesPlanJiedianDO.builder()
                    .jx(CommonUtils.stringNull(mesPlanJiedianDTO.getJx()))
                    .partid(CommonUtils.stringNull(mesPlanJiedianDTO.getPartid()))
                    .gongxuId(CommonUtils.stringNull(mesPlanJiedianDTO.getGongxuId()))
                    .crerateId(CommonUtils.stringNull(mesPlanJiedianDTO.getCrerateId()))
                    .zjgxnr(CommonUtils.stringNull(mesPlanJiedianDTO.getZjgxnr()))
                    .jgdw(CommonUtils.stringNull(mesPlanJiedianDTO.getJgdw()))
                    .worktime(mesPlanJiedianDTO.getWorktime())
                    .build());
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateJiedian(MesPlanJiedianDTO mesPlanJiedianDTO) {
        int repeat = this.mesPlanJiedianMapper.repeatJiedian(MesPlanJiedianDO.builder()
                .jx(CommonUtils.stringNull(mesPlanJiedianDTO.getJx()))
                .partid(CommonUtils.stringNull(mesPlanJiedianDTO.getPartid()))
                .gongxuId(CommonUtils.stringNull(mesPlanJiedianDTO.getGongxuId()))
                .build());
        int count = 0;
        if (repeat == 0) {
            count = this.mesPlanJiedianMapper.updateJiedian(MesPlanJiedianDO.builder()
                    .objuid(CommonUtils.stringNull(mesPlanJiedianDTO.getObjuid()))
                    .jx(CommonUtils.stringNull(mesPlanJiedianDTO.getJx()))
                    .partid(CommonUtils.stringNull(mesPlanJiedianDTO.getPartid()))
                    .gongxuId(CommonUtils.stringNull(mesPlanJiedianDTO.getGongxuId()))
                    .modifyId(CommonUtils.stringNull(mesPlanJiedianDTO.getModifyId()))
                    .zjgxnr(CommonUtils.stringNull(mesPlanJiedianDTO.getZjgxnr()))
                    .jgdw(CommonUtils.stringNull(mesPlanJiedianDTO.getJgdw()))
                    .worktime(mesPlanJiedianDTO.getWorktime())
                    .build());
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteJiedian(MesPlanJiedianDTO mesPlanJiedianDTO) {
        int count = 0;
        count = this.mesPlanJiedianMapper.insertJiedian(MesPlanJiedianDO.builder()
                .objuid(CommonUtils.stringNull(mesPlanJiedianDTO.getObjuid()))
                .deleteId(CommonUtils.stringNull(mesPlanJiedianDTO.getModifyId()))
                .build());
        return count;
    }

    @Override
    public int exportJiedian(MesPlanJiedianDTO mesPlanJiedianDTO) {
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void uploadJiedian(List<MesPlanJiedianDO> list) {
        SqlSession sqlSession = this.sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
        MesPlanJiedianMapper mesPlanJiedianMapper = sqlSession.getMapper(MesPlanJiedianMapper.class);
        list.forEach(a -> {
            mesPlanJiedianMapper.insert(a);
        });
        sqlSession.commit();
    }
}
